package com.dgut.cbd.service;

import com.dgut.cbd.data.DataContainer;
import com.dgut.cbd.entity.DataModel;
import com.dgut.cbd.result.RO;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Program: exp3
 * @Description:
 * @Author: cbd
 * @Create: 2021-05-25
 * @Version: 1.0.0
 **/
@Service
@Slf4j(topic = "myLog")
public class MainService {

    @Autowired
    DataContainer dataContainer;

    public List<String> searchState(String country) {
        Map<String, List<DataModel>> countryDataMap = dataContainer.getCountryDataMap();
        List<DataModel> dataModels = countryDataMap.get(country);
        if (dataModels == null)
            return null;
        List<String> collect = dataModels.stream().map(e -> e.getState()).collect(Collectors.toList());
        return collect;
    }

    public List<DataModel> getDateModels(String country,String state,Date startDate,Date endDate){

        List<DataModel> dataModels = dataContainer.getCountryDataMap().getOrDefault(country, new LinkedList<>());
        String finalState = state;  // 前面对state的赋值破坏了effective final



        if (!"all".equals(state)) {
            dataModels = dataModels.stream()
                    .filter(e -> e.getState().equals(new String(finalState)))
                    .collect(Collectors.toList());
        }



        if (startDate != null && endDate != null) {

            // bad design causes bad code
            int start = -1, end = -1;
            List<String> date = dataModels.get(0).getDate();
            for (int i = 0; i < date.size(); i++) {
                String[] split = date.get(i).split("/");
                if (Integer.valueOf(split[0]) == startDate.getMonth() + 1
                        && Integer.valueOf(split[1]) == startDate.getDate()
                        && Integer.valueOf(split[2]) == startDate.getYear() + 1900) {
                    start = i;
                }
                if (Integer.valueOf(split[0]) == endDate.getMonth() + 1
                        && Integer.valueOf(split[1]) == endDate.getDate()
                        && Integer.valueOf(split[2]) == endDate.getYear() + 1900) {
                    end = i;
                    break;
                }
            }

            if (start != -1 || end != -1) {

                if (start == -1)
                    start = 0;
                if (end == -1)
                    end = date.size() - 1;

                ObjectMapper objectMapper = new ObjectMapper();
                dataModels = objectMapper.convertValue(dataModels, LinkedList.class);
                for (int i = 0; i < dataModels.size(); i++) {

                    dataModels.set(i, objectMapper.convertValue(dataModels.get(i), DataModel.class));
                    DataModel t = dataModels.get(i);
                    t.setDate(t.getDate().subList(start, end));
                    t.setNums(t.getNums().subList(start, end));
                    t.setTotal(t.getTotal().subList(start, end));
                }
            }

        }
        return dataModels;
    }
}
